<?php


namespace App\Models\CacheModel;


use App\Models\Api\BaseCache;
use AstroTool\Cache\CacheManage;

class ZlcrTag extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zlcr_tag';

    protected $cacheModel = 'custom_report.question_comment_tag';

    /**
     * 获取不同评级的标签
     * @param $type 1=>定制报告商品标签，2=>定制报告评论标签'
     * @param $level
     * @return array|bool|mixed|string
     * @throws \Exception
     */
    public function getLevelTag($type, $level)
    {
        $ids = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $level)->setListKeyType($type)->setSort('DESC')->ZsGetIocData('list', function () use ($level, $type)
        {
            if ($level)
            {
                $where = ['tag_type' => $type, 'flag' => $level, 'status' => 1];
            } else
            {
                $where = ['tag_type' => $type, 'status' => 1];
            }
            try
            {
                $data = $this->where($where)->order('sort desc')->column('sort', 'id');
            }
            catch (\Exception $e)
            {
                $data = [];
            }
            return $data;
        }, true);
        return $this->batchGetInfoByIds(array_keys($ids));
    }


    /**
     * 获取所有标签并重组
     * @param $data
     * @return array
     */
    public function getQuestionAllsResrt($data): array
    {
        $resData = [];
        foreach ($data as $value)
        {
            if (!isset($resData[$value['flag']]))
            {
                $resData[$value['flag']] = [
                    'level' => (int)$value['flag'],
                    'tags' => []
                ];
            }
            $resData[$value['flag']]['tags'][] = [
                'id' => (int)$value['id'],
                'tname' => $value['tag_name'],
            ];
        }
        ksort($resData);
        return $resData ? array_values($resData) : $resData;
    }

    /**
     * 获取对应的问题评论信息
     * @param array $ids
     * @param array|null $type
     * @param string $sortType
     * @return array
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        $where = ['id' => ['in', $ids], 'status' => 1];
        $field = 'flag,tag_name,remark,sort,score,templet_url,extend';
        try
        {
            $data = $this
                ->field($field)
                ->where($where)
                ->order('sort desc,id desc')
                ->column($field, 'id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }
        return $data;
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        foreach ($resData as $orderId => $orderItemData)
        {
            if (isset($orderItemData['extend']))
            {
                $orderItemData['extend'] = $orderItemData['extend'] ? json_decode($orderItemData['extend'], true) : [];
            }

            $resData[$orderId] = arrayKeyData2int($orderItemData, ['id', 'flag', 'sort', 'score']);
        }

        return $resData;
    }
}
